package main

import "fmt"

type User struct {
	name    string
	passwd  string
	balance int
}

type ATM struct {
	bank    string
	balance int
}

func (atm *ATM) check_balance(user *User) {
	fmt.Printf("%s的余额为%d\n", user.name, user.balance)
}

func (atm *ATM) save_money(user *User, money int) {
	user.balance += money
	atm.balance += money
	fmt.Printf("存入成功！余额为%d\n", user.balance)
}

func (atm *ATM) get_money(user *User, money int) {
	if user.balance < money {
		fmt.Println("账户余额不足")
	} else if atm.balance < money {
		fmt.Println("ATM余额不足")
	} else {
		user.balance -= money
		atm.balance -= money
		fmt.Printf("取款成功余额为%d\n", user.balance)
	}
}

func main() {
	user_list := make(map[string]*User)
	atm := ATM{
		bank:    "建设银行",
		balance: 500000,
	}
	var op, option, name, passwd, new_name, new_passwd string
	var money, new_money int
	for {
		fmt.Printf("1.登录\n2.注册\n按q退出\n")
		println("请输入你的选择:")
		fmt.Scanln(&op)
		if op == "q" {
			break
		}
		if op == "1" {
			println("请输入用户名:")
			fmt.Scanln(&name)
			println("请输入密码:")
			fmt.Scanln(&passwd)
			if _, ok := user_list[name]; ok && passwd == user_list[name].passwd {
				fmt.Println("登录成功！")
				for {
					fmt.Printf("1.查看余额\n2.取款\n3.存款\n4.退出\n")
					println("请输入你的操作:")
					fmt.Scanln(&option)
					if option == "1" {
						atm.check_balance(user_list[name])
					} else if option == "2" {
						println("请输入取款金额:")
						fmt.Scanln(&money)
						atm.get_money(user_list[name], money)
					} else if option == "3" {
						println("请输入存款金额:")
						fmt.Scanln(&money)
						atm.save_money(user_list[name], money)
					} else if option == "4" {
						break
					} else {
						fmt.Println("输入有误")
					}
				}
			} else {
				fmt.Println("用户名或密码错误！")
			}
		} else if op == "2" {
			println("请输入用户名:")
			fmt.Scanln(&new_name)
			if _, ok := user_list[new_name]; !ok {
				println("请输入密码:")
				fmt.Scanln(&new_passwd)
				println("充值初始金额:")
				fmt.Scanln(&new_money)
				user := &User{
					name:    new_name,
					passwd:  new_passwd,
					balance: new_money,
				}
				user_list[new_name] = user
			} else {
				fmt.Println("用户名已存在！请重新输入")
			}
		} else {
			fmt.Println("输入不合法！")
		}
	}
}
